package com.ynny.dao;

import com.mysql.cj.x.protobuf.MysqlxExpr;
import com.ynny.bean.student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.time.DayOfWeek;
import java.util.List;

@Component("studentManage")
public class StudentManageImpl implements StudentManage{
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public void addStudent(student student) {
        String sql = "insert into student(number,name,grade,clbum,gender) values(?,?,?,?,?)";
        Object[] obj = new Object[]{
                student.getNumber(),student.getName(),student.getGrade(),student.getClbum(),student.getGender()
        };
        int num = jdbcTemplate.update(sql,obj);
        if(num>0){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }

    }

    @Override
    public void updateStudent(String number, String name, String grade, String clbum, String gender) {
        String sql = "update student set name=?,grade=?,clbum=?,gender=? where number=?";
        Object[] obj = new Object[]{
                name,grade,clbum,gender,number
        };
        int num = jdbcTemplate.update(sql,obj);
        if(num>0){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }

    }

    @Override
    public void queryStudentByNum(String number) {
        String sql = "select * from student where number=?";
        RowMapper<student> rowMapper;
        rowMapper = new BeanPropertyRowMapper<student>(student.class);
        student student = jdbcTemplate.queryForObject(sql, rowMapper,number);
        System.out.println(student);
        }

    @Override
    public void queryAllStudent() {
        String sql = "select * from student";
        RowMapper<student> rowMapper;
        rowMapper = new BeanPropertyRowMapper<student>(student.class);
        List<student> studentList = jdbcTemplate.query(sql, rowMapper);
        for (student student : studentList) {
            System.out.println(student);
        }

    }



    }



